package com.gather.treemaps;

import com.gather.orm.House;
import java.util.Comparator;
import java.util.TreeMap;

public class Example03 {
    public static void main(String[] args) {
        System.out.println("梦想还是要有的，万一实现了呢");
        TreeMap<House, String> houseStringTreeMap = new TreeMap<>(new Comparator<House>() {
            @Override
            public int compare(House o1, House o2) {
                // 自定义排序 小到大排序
                int n = o1.getPrice();
                int m = o2.getPrice();
                String name1 = o1.getName();
                String name2 = o2.getName();
                //  先比较房子
                int i = name1.compareTo(name2);
                int k = n - m;
                // 房子名称 和 价钱一样才是同一个key
                if (i == 0  && k == 0) {
                    return 0;
                } else {
                    if (i == 0) {
                        // 名字相同  价钱不同，价格低排前面
                        return (k > 0) ? 1 : -1;
                    } else {
                        // 名字不同，按照字母排序
                        return (i > 0) ? 1: -1 ;
                    }
                }
            }
        });
        House h1 = new House("x",20);
        House h2 = new House("b",30);
        House h3 = new House("h",10);
        // 添加
        houseStringTreeMap.put(h1,"碧桂园");
        houseStringTreeMap.put(h2,"万达");
        houseStringTreeMap.put(h3,"恒大");
        // 无法添加重复元素 -- 无法添加
        houseStringTreeMap.put(h3,"恒大");
        houseStringTreeMap.put(new House("h",10),"恒大");
        // 下面只能添加成功一个。 名字相同，价格不同
        houseStringTreeMap.put(new House("b",12),"恒大");
        houseStringTreeMap.put(new House("b",12),"恒大");
        houseStringTreeMap.put(new House("b",13),"恒大");
        // {b:12=恒大, b:13=恒大, b:30=万达, h:10=恒大, x:20=碧桂园}
        System.out.println(houseStringTreeMap.toString());
    }
}
